package com.ropeok.Service.ServiceImpl;

import com.ropeok.Service.service;
import com.ropeok.dao.impl.PermissionDaoImpl;
import com.ropeok.dao.impl.RoleDaoImpl;
import com.ropeok.dao.impl.UserAndRoleDaoImpl;
import com.ropeok.dao.impl.UserDaoImpl;
import com.ropeok.entity.Role;
import com.ropeok.entity.User;
import com.ropeok.util.c3p0Util;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class serviceimpl implements service {

//    static Connection coon=c3p0Util.getConnection();

    public List<User> list(){
        Connection coon=null;
        try{
            coon= c3p0Util.getConnection();
            coon.setAutoCommit(false);
            UserDaoImpl userDao=new UserDaoImpl();
            List<User> userList=userDao.FindAll();
//            int i=1/0;//测试事务
            RoleDaoImpl roleDao=new RoleDaoImpl();
            PermissionDaoImpl permissionDao=new PermissionDaoImpl();

            for(User user:userList){
                user.setLrole(roleDao.findByUserid(user.getUserid()));
                List<Role> lrole = user.getLrole();
                for (Role role:lrole)
                    user.setLpermissions(permissionDao.findByRoleId(role.getRoleid()));
            }
//            int i=1/0;
            coon.commit();
            return userList;
        }catch (Exception e){
            try {
                System.out.println("执行了事务");
                coon.rollback();
            }catch (SQLException e1){
                e1.printStackTrace();
            }
            e.printStackTrace();
            throw new RuntimeException("获取信息失败，请检查网络！");//添加这条语句可以不用在最后加return null
        }finally {
            c3p0Util.close(null,coon,null);
        }
//        return null;
    }

    @Override
    public void DelUser(Integer id) {
        Connection coon=null;
        try{
            coon=c3p0Util.getConnection();
            coon.setAutoCommit(false);
            UserDaoImpl userDao=new UserDaoImpl();
            UserAndRoleDaoImpl userAndRoleDao=new UserAndRoleDaoImpl();
            userDao.DelUser(id);
            userAndRoleDao.DelURUser(id);
            coon.commit();
        }catch (Exception e){
            try{
                System.out.println("执行了事务");
                coon.rollback();
            }catch (SQLException e2){
                e2.printStackTrace();
            }
            e.printStackTrace();
            throw new RuntimeException("获取信息失败，请检查网络！");
        }
    }



    /*
    * 测试事务能否rollback的代码
    * */
    @Override
    public void saves(User user) {
        Connection coon=null;
        try {
            coon=c3p0Util.getConnection();
            coon.setAutoCommit(false);
            UserDaoImpl uD=new UserDaoImpl();
//            RoleDaoImpl rD=new RoleDaoImpl();
//            rD.Save(role);
            uD.Save(user);
//            int i=1/0;
            coon.commit();
        }catch (Exception e){
            try {
                System.out.println("执行了事务");
                coon.rollback();
            }catch (SQLException e3){
                System.out.println("执行了事务B");
                e3.printStackTrace();
            }
            e.printStackTrace();
            throw new RuntimeException("获取信息失败，请检查网络！");
        }
    }

}
